#!/usr/bin/python2.6
import salt.grains.core
import os
import MySQLdb
import commands
def CPIS():
    hostname=salt.grains.core.hostname()['fqdn']
    for ip in salt.grains.core.ip4()['ipv4']:
       if not ip.startswith('127') and not ip.startswith('172'):
           hostip=ip
    cpuinfo=salt.grains.core.os_data()['cpu_model']
    meminfo=salt.grains.core.os_data()['mem_total']
    saltversion=salt.grains.core.saltversion()['saltversion']
    pythonversion=commands.getoutput('python -V')
    os=salt.grains.core.os_data()['osfullname'] + salt.grains.core.os_data()['osrelease']
    kernelrelease=salt.grains.core.os_data()['kernelrelease']

    conn=MySQLdb.connect(host='XXXXX',user='root',passwd='XXXXXX',db='host',port=3306)
    cursor=conn.cursor()
    cursor.execute('INSERT INTO info VALUES (%s,%s,%s,%s,%s,%s,%s,%s)' % ('"'+str(hostname)+'"','"'+str(hostip)+'"','"'+str(cpuinfo)+'"','"'+str(meminfo)+'"','"'+str(saltversion)+'"','"'+str(pythonversion)+'"','"'+str(os)+'"','"'+str(kernelrelease)+'"'))
    conn.commit()
    cursor.close()
    conn.close()
#    print hostname,hostip,cpuinfo,meminfo,saltversion,pythonversion,os,kernelrelease
